package some.message.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import some.message.dao.MessageDao;

@Controller
public class MessageDeleteController {
	
	@Autowired
	private MessageDao messageDao;
	
	@RequestMapping("messageDelete.do")
	public String process(@RequestParam("num")int num,HttpSession session){

		String id = (String)session.getAttribute("id");
		String pid = messageDao.getMessage(num).getSenderid();
		
		Map<String,Object> ids = new HashMap<String,Object>();
		ids.put("num", num);
		
		
		if(id.equals(pid)){
			ids.put("sendMessage","senderid");
		}else{
			ids.put("sendMessage","receiverid");
		}
		if(num==0||num<0){
			return "redirect:/messageForm.do?method="+ids.get("sendMessage");
		}
		
		messageDao.updateMessage(ids);
		
		
		return "redirect:/messageForm.do?method="+ids.get("sendMessage");
	}
	
}
